package cn.edu.zafu.bigdata.bd121.except;

import java.util.Scanner;

/**
 * 分数异常demo
 *
 * @author 202211090121
 */
public class ScoreExceptionExample {

    private static final int MAX_SCORE = 100;

    /**
     * 检查分数是否有效
     *
     * @param score 分数
     * @throws InvalidScoreException 分数异常
     */
    public static void checkScore(int score) throws InvalidScoreException {
        if (score < 0 || score > MAX_SCORE) {
            throw new InvalidScoreException("错误: 分数必须在0到" + MAX_SCORE + "之间。");
        }
    }

    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            int score;
            System.out.print("请输入分数: ");
            score = scanner.nextInt();
            checkScore(score);
            System.out.println("分数有效。分数为: " + score);
        } catch (InvalidScoreException e) {
            System.err.println(e.getMessage());
        } catch (Exception e) {
            System.err.println("错误: 请输入一个有效的整数。");
        }
    }
}
